//
//  MineViewController.m
//  QLadder
//
//  Created by xiongguobing on 2019/7/30.
//  Copyright © 2019 qding. All rights reserved.
//

#import "MineViewController.h"
#import "GoldenVPNLoginVC.h"
#import "GoldenVPNServiceListVC.h"
#import "NSString+VerifyString.h"


@interface MineViewController ()
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *thisMouthLabel;
@property (weak, nonatomic) IBOutlet UILabel *lastMouthLabel;
@property (weak, nonatomic) IBOutlet UILabel *serviceTypeLabel;
@property (weak, nonatomic) IBOutlet UILabel *expireTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *todayUsedLabel;
@property (weak, nonatomic) IBOutlet UILabel *yestodayUsedLabel;

@property (weak, nonatomic) IBOutlet UITableViewCell *serviceCell;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadUserInfo];
    // Do any additional setup after loading the view.
}

- (void)loadUserInfo
{
    NSString * userName = [kUserDefaults objectForKey:kUserAccount];
    if (userName) {
        [kNetworkUtil getWithModuleURLStr:userData parameters:@{@"userName" : userName} success:^(id response) {
            if ([[response objectForKey:@"code"] integerValue] == networkSuccessStatus) {
                NSDictionary * dic = response[@"useinfoVO"];
                self.nameLabel.text = dic[@"nickname"];
                self.thisMouthLabel.text = [NSString stringWithFormat:@"%@M", dic[@"montyUseflow"]];
                self.lastMouthLabel.text = [NSString stringWithFormat:@"%@M", dic[@"lastMonthUseflow"]];
                
                if (ValidStr(dic[@"productName"])) {
                    self.serviceTypeLabel.text = dic[@"productName"];
                }else{
                    self.serviceTypeLabel.textColor = RedColor;
                }
                double expireTime = [dic[@"vpnExpireTime"] doubleValue];
                self.expireTimeLabel.text = [NSString timeWithTimestamp:expireTime];
                
                self.todayUsedLabel.text = [NSString stringWithFormat:@"%@M", dic[@"todayUseflow"]];
                self.yestodayUsedLabel.text = [NSString stringWithFormat:@"%@M", dic[@"yesterdayUseflow"]];
                
                //提示是不是付费用户
                if (dic[@"openVpn"]) {
                    if ([dic[@"openVpn"] integerValue] > 0) {
                        self.nameLabel.text = [self.nameLabel.text stringByAppendingString:@"(付费用户)"];
                    }else{
                        self.nameLabel.text = [self.nameLabel.text stringByAppendingString:@"(非付费用户)"];
                    }
                }
            }
        } failure:nil withHud:YES];
    }
}

- (IBAction)loginOutButtonClicked:(UIButton *)sender
{
    [HUDUtil showWithTitle:@"确定要退出登录吗？" viewController:self handler:^(UIAlertAction *action) {
        [kNotificationCenter postNotificationName:@"willLoginOutNotification" object:nil];
        NSString * account = [kUserDefaults objectForKey:kUserAccount];
        NSString * pwd = [kUserDefaults objectForKey:kUserPassword];
        if (ValidStr(account) && ValidStr(pwd)) {
            [kNetworkUtil postWithModuleURLStr:loginOut parameters:@{@"passWord" : pwd, @"userName" : account} success:^(id response) {
                if ([[response objectForKey:@"code"] integerValue] == 200) {
                    [kUserDefaults removeObjectForKey:kUserPassword];
                    
                    GoldenVPNLoginVC * loginVC = [GoldenVPNLoginVC new];
                    UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
                    kAppWindow.rootViewController = nav;
                }
            } failure:nil withHud:YES];
        }
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 3) {
        [self performSegueWithIdentifier:@"mineVCToServiceVCSegue" sender:self];
    }
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}


@end
